﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace RestaurateurConsole
{
    public partial class LogViewer : Form
    {
        bool showConfig;
        public LogViewer()
        {
            InitializeComponent();
            showConfig = false;
        }

        public LogViewer(bool ShowConfig)
        {
            InitializeComponent();
            showConfig = true;
        }

        bool customlog = false;
        public LogViewer(byte[] data)
        {
            customlog = true;
            InitializeComponent();
            MemoryStream ms = new MemoryStream(data);
            StreamReader rd = new StreamReader(ms);
            textBox1.Text = rd.ReadToEnd();
            rd.Close();
            toolStripButton1.Enabled = false;
        }

        private void LogViewer_Load(object sender, EventArgs e)
        {
            if (!customlog)
                LoadData();
        }

        private void LoadLogs()
        {
            this.Text = "View SQL Logs...";
            textBox1.Clear();
            MemoryStream ms = new MemoryStream(DataEncapsulator.Stream);
            StreamReader reader = new StreamReader(ms);
            textBox1.Text = reader.ReadToEnd();
            textBox1.SelectAll();
            textBox1.Select(textBox1.SelectionLength - 1, 0);
            textBox1.ScrollToCaret();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void LoadData()
        {
            if (!showConfig)
                LoadLogs();
            else
                LoadConfig();
        }

        private void LoadConfig()
        {
            this.Text = "View Config Items...";
            textBox1.Clear();
            textBox1.Text += "First Run:\t" + ConfigHandler.Configuration.FirstRun.ToString();
            textBox1.Text += "\r\nKey:\t" + ConfigHandler.Configuration.RegistrationKey;
            textBox1.Text += "\r\nLast Unregistered Run Date:\t" + ConfigHandler.Configuration.LastRunDate.ToString();

            textBox1.Text += "\r\n\r\nAddress:\r\n";
            foreach (var v in ConfigHandler.Configuration.Address)
                textBox1.Text += "\t" + v + "\r\n";
            textBox1.Text += "\r\nPhone:\t" + ConfigHandler.Configuration.PhoneNumber;

            textBox1.Text += "\r\n\r\nNumber of tables:\t" + ConfigHandler.Configuration.NoOfTables.ToString();
            textBox1.Text += "\r\nTax Addition Threshold:\t" + ConfigHandler.Configuration.TaxAdditionThreshhold.ToString();
            textBox1.Text += "\r\nTax Rate:\t" + ConfigHandler.Configuration.TaxRate.ToString();

            textBox1.Text += "\r\n\r\nSalesID:\t" + ConfigHandler.Configuration.SalesID.ToString();
            textBox1.Text += "\r\nSoldItemsID:\t" + ConfigHandler.Configuration.SoldItemsID.ToString();

            textBox1.Text += "\r\n\r\nUnused SalesIDs:\r\n";
            foreach (var v in ConfigHandler.Configuration.UnusedSalesID)
                textBox1.Text += "\t" + v.ToString() + "\r\n\r";

            //Check for duplicates
            if (ConfigHandler.Configuration.UnusedSalesID.Count > 0)
            {
                int[] cache = new int[ConfigHandler.Configuration.UnusedSalesID.Max() + 1];
                for (int i = 0; i < cache.Length; ++i)
                    cache[i] = 0;

                foreach (var v in ConfigHandler.Configuration.UnusedSalesID)
                    cache[v]++;

                bool cleanup = false;
                for (int i = 0; i < cache.Length; ++i)
                    if (cache[i] > 1)
                    {
                        cleanup = true;
                        textBox1.Text += "Duplicate values present in unusedSalesIDs. Cleaning up.";
                        break;
                    }

                if (cleanup)
                {
                    ConfigHandler.Configuration.UnusedSalesID.Clear();
                    for (int i = 0; i < cache.Length; ++i)
                        if (cache[i] != 0)
                            ConfigHandler.Configuration.UnusedSalesID.Push(i);

                    ConfigHandler.Configuration.Save();

                    textBox1.Text += "\r\n\r\nUnused SalesIDs:\r\n";
                    foreach (var v in ConfigHandler.Configuration.UnusedSalesID)
                        textBox1.Text += "\t" + v.ToString() + "\r\n";
                }
            }
        }
    }
}
